﻿#include "reject.h"
#include "ui_reject.h"

Reject::Reject(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Reject)
{
    ui->setupUi(this);
}

Reject::~Reject()
{
    delete ui;
}

bool Reject::init()
{
    int state=0;
    if(option.load(LST_EXPRESS))
    {
        ui->express->addItems(option.getStringList());
        ui->express->addItem(OPT_EDIT);
        ui->express->setCurrentIndex(-1);
        state++;
    }
    if(option.load(LST_TRANSACTOR))
    {
        ui->transactor->addItems(option.getStringList());
        ui->transactor->addItem(OPT_EDIT);
        ui->transactor->setCurrentIndex(-1);
        state++;
    }

    if(state == 2)
        return true;
    else
        return false;
}

bool Reject::saveRecord()
{
    QSqlQuery query;
    query.prepare("insert into td_reject (date,contact,content,express,transactor,remark) values (?,?,?,?,?,?)");
    query.addBindValue(Env::date);
    query.addBindValue(contact);
    query.addBindValue(content);
    query.addBindValue(express);
    query.addBindValue(transactor);
    query.addBindValue(remark);
    return query.exec();
}

void Reject::on_btn_submit_clicked()
{
    if(ui->contact->text().isEmpty() && ui->phone->text().isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("联系人、手机号不能都为空！"),QMessageBox::Ok);
        return;
    }

    content=ui->content->toPlainText();
    if(content.isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("退货详情不能为空！"),QMessageBox::Ok);
        return;
    }

    contact=ui->contact->text().trimmed() + " " + ui->phone->text().trimmed();
    remark=ui->remark->text();
    express=ui->express->currentText() + " " + ui->expressNum->text();
    transactor=ui->transactor->currentText();

    if(saveRecord())
    {
        ui->contact->clear();
        ui->phone->clear();
        ui->content->clear();
        ui->remark->clear();
        ui->lbl_status->setText(QStringLiteral("记录已提交"));
    }
    else
        ui->lbl_status->setText(QStringLiteral("提交失败！数据库异常"));
}

void Reject::reload(const OptionType &type)
{
    if(type == O_EXPRESS)
    {
        if(option.load(LST_EXPRESS))
        {
            ui->express->clear();
            ui->express->addItems(option.getStringList());
            ui->express->addItem(OPT_EDIT);
        }
    }
    else if(type == O_TRANSACTOR)
    {
        if(option.load(LST_TRANSACTOR))
        {
            ui->transactor->clear();
            ui->transactor->addItems(option.getStringList());
            ui->transactor->addItem(OPT_EDIT);
        }
    }
}

void Reject::on_transactor_currentTextChanged(const QString &text)
{
    if(text == OPT_EDIT)
    {
        OptionEditor editor;
        if(editor.detect(O_TRANSACTOR))
        {
            connect(&editor,SIGNAL(reload(OptionType)),this,SLOT(reload(OptionType)));
            if(editor.exec() == QDialog::Rejected)
                ui->transactor->setCurrentIndex(-1);
        }
        else
            QMessageBox::critical(this,QStringLiteral("错误"),QStringLiteral("无法加载配置文件！"),QMessageBox::Ok);
    }
}

void Reject::on_express_currentTextChanged(const QString &text)
{
    if(text == OPT_EDIT)
    {
        OptionEditor editor;
        if(editor.detect(O_EXPRESS))
        {
            connect(&editor,SIGNAL(reload(OptionType)),this,SLOT(reload(OptionType)));
            if(editor.exec() == QDialog::Rejected)
                ui->express->setCurrentIndex(-1);
        }
        else
            QMessageBox::critical(this,QStringLiteral("错误"),QStringLiteral("无法加载配置文件！"),QMessageBox::Ok);
    }
}
